package org.duiduo.aspect;

import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.Signature;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.aspectj.lang.reflect.MethodSignature;
import org.duiduo.annotation.AuthLogin;
import org.springframework.stereotype.Component;

/**
 * 登录验证的切面
 */
@Slf4j
@Aspect
@Component
public class AutoAspect {

    @Pointcut(value = "@annotation(org.duiduo.annotation.AuthLogin)")
    public void access() {
    }

    // 前置通知
    @Before("access()")
    public void before() {
        log.info("开始验证用户是否登录...");
    }

    @Around("@annotation(org.duiduo.annotation.AuthLogin)")
    public Object around(ProceedingJoinPoint proceedingJoinPoint) {
        Signature signature = proceedingJoinPoint.getSignature();
        MethodSignature methodSignature = (MethodSignature) signature;
        AuthLogin authLogin = methodSignature.getMethod().getAnnotation(AuthLogin.class);
        // 获取注解中的值
        log.info("注解中的值:{}", authLogin.needLogin());
        try {
            // 检验是否登录 true 已经登录  false 未登录
            boolean flag = authLogin.needLogin();
            if (!flag) {
                log.info("登录状态：未登录");
            } else {
                log.info("登录状态：登录成功");
            }
            Object o = proceedingJoinPoint.proceed();
            return o;
        } catch (Throwable throwable) {
            return null;
        }
    }

}
